<template>
  <div>
    昂昂昂
    <div class="container">
      <input
        type="file"
        accept=".xls,.xlsx"
        class="upload_file"
        @change="handleUpload($event)"
      />
    </div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="88px"
      style="margin-top: 20px"
    >
      <el-form-item label="使用状态" prop="couponIsUsed">
        <el-select
          v-model="queryParams.couponIsUsed"
          clearable
          placeholder="请选择"
        >
          <el-option label="已使用" value="1"></el-option>
          <el-option label="未使用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="核销手机号" prop="verificationUserPhone">
        <el-input
          v-model="queryParams.verificationUserPhone"
          placeholder="请输入手机号"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="领取时间" prop="getCouponTime">
        <el-date-picker
          style="width: 350px"
          v-model="dateRangeCreatedDate"
          size="mini"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button
          type="info"
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          >重置</el-button
        >
        <!--
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
          >导入</el-button
        >

        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
          >删除</el-button
        >
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
        -->
      </el-form-item>
    </el-form>
    <el-table :data="couponUserList">
      <af-table-column label="发放方式标识" align="left" prop="issueRemark" />
      <af-table-column label="活动名称" align="left" prop="issueName" />

      <af-table-column
        label="手机"
        align="left"
        prop="couponRemark"
        fixed="left"
      />
      <af-table-column label="等级" align="left">
        <template slot-scope="scope">
          {{
            scope.row.couponType == 1
              ? "现金劵" + "--面值：" + scope.row.couponAmt + "元"
              : ""
          }}
          {{
            scope.row.couponType == 2
              ? "折扣券" + "--折扣：" + scope.row.couponAmt + "% "
              : ""
          }}
          {{
            scope.row.couponType == 3
              ? "兑换券" + "--商品：" + scope.row.couponName
              : ""
          }}
        </template>
      </af-table-column>
      <af-table-column label="标签" align="left">
        <template slot-scope="scope">
          {{ scope.row.couponIsUsed == "0" ? "未使用" : "" }}
          {{ scope.row.couponIsUsed == "1" ? "已使用" : "" }}
          {{ scope.row.couponIsUsed == "2" ? "已核销" : "" }}
        </template>
      </af-table-column>
      <af-table-column label="优惠券" align="left">
        <template slot-scope="scope">
          {{ scope.row.couponIsEffective == "0" ? "无效" : "" }}
          {{ scope.row.couponIsEffective == "1" ? "有效" : "" }}
        </template>
      </af-table-column>
      <af-table-column
        label="用户保存id"
        align="left"
        prop="userId"
        v-if="false"
      />
      <af-table-column label="电子卡余额" align="left" prop="mobilePhone" />
      <af-table-column label="积分余额" align="left" prop="blogNickName" />
      <af-table-column
        label="核销员手机号"
        align="left"
        prop="verificationUserPhone"
      />
      <af-table-column
        label="优惠券截止时间"
        align="left"
        prop="couponEffectiveTime"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.couponEffectiveTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </af-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { listOrder, exportOrder, printOrderInfo } from "@/api/station/order";
import { stationinfo } from "@/api/station/gun";
import { listPrice, getPrice } from "@/api/station/price";
import { queryCouponUserList } from "@/api/coupon";
import { listManage } from "@/api/station/manage";
import excel from "@/utils/excel";

export default {
  name: "coupon_user",
  data() {
    return {
      //创建订单时间间隔
      dateRangeCreatedDate: [],
      // 总条数
      total: 0,
      // 优惠券的信息
      couponUserList: [],
      oilNameOptions: [],
      // 字典
      payTypeOptions: [],
      // 下拉油站
      stationOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        levelId: this.levelId,
        pageNum: 1,
        pageSize: 10,
        issueId: null,
        couponId: null,
        stationId: null,
        couponIsUsed: null,
        couponIsEffective: null,
        getCouponTime: null,
        verificationUserPhone: null,
      },
      // 表单校验
      rules: {},
      reprint: false,
    };
  },
  created() {
    this.getList();
  },

  methods: {
    handleUpload(e) {
      console.log("上传");
      // console.log(e);
      const input = e.target;
      // console.log(input);
      const files = e.target.files;
      let file = files[0];
      // console.log(file);
      const fileExt = file.name.split(".").pop().toLocaleLowerCase();
      if (fileExt === "xlsx" || fileExt === "xls") {
        this.readFile(file);
        this.file = file;
      } else {
        this.msgError("请选择后缀为.xlsx或者.xls的EXCEL文件");
      }
      return false;
    },
    // 读取文件转换json
    readFile(file) {
      const reader = new FileReader();
      reader.readAsArrayBuffer(file);
      reader.onloadstart = (e) => {
        console.log("onloadstart");
        // this.tableLoading = true;
      };
      reader.onprogress = (e) => {
        console.log("onprogress");
        this.progressPercent = Math.round((e.loaded / e.total) * 100);
      };
      reader.onerror = (e) => {
        this.msgError("文件读取出错");
      };
      reader.onload = (e) => {
        console.log("onload");
        const data = e.target.result;
        const { results } = excel.read(data, "array");
        //这里的tableData就是拿到的excel表格中的数据
        console.log("results", results);
      };
    },
    /** 查询订单支付列表 */
    getList() {
      this.queryParams.levelId = this.levelId;
      queryCouponUserList(
        this.addDateRange(this.queryParams, this.dateRangeCreatedDate)
      ).then((response) => {
        this.couponUserList = response.rows;
        this.total = response.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreatedDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有订单支付数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportOrder(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
